﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;
using CityDashboard.Models;
using CityDashboard.Services;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Prism.Mvvm;

namespace CityDashboard.ViewModels
{
    public class HoSoChiTietViewModel : ViewModel
    {

        private bool _trinhUB;
        public bool TrinhUB
        {
            get { return _trinhUB; }
            set
            {
                _trinhUB = value;
                if (value == false && _kotrinhUB == false)
                {
                    KoTrinhUB = true;
                }
                OnPropertyChanged("TrinhUB");
            }
        }


        private bool _daXuLy;
        public bool DaXuLy
        {
            get { return _daXuLy; }
            set
            {
                _daXuLy = value;
                if (value == false && _kodaXuLy == false)
                {
                    KoDaXuLy = true;
                }

                OnPropertyChanged("DaXuLy");
            }
        }

        private bool _dungHan;
        public bool DungHan
        {
            get { return _dungHan; }
            set
            {
                _dungHan = value;
                if (value == false && _kodungHan == false)
                {
                    KoDungHan = true;
                }
                OnPropertyChanged("DungHan");
            }
        }

        private bool _kodungHan;
        public bool KoDungHan
        {
            get { return _kodungHan; }
            set
            {
                _kodungHan = value;
                if (value == false && _dungHan == false)
                {
                    DungHan = true;
                }
                OnPropertyChanged("KoDungHan");
            }
        }

        private bool _kotrinhUB;
        public bool KoTrinhUB
        {
            get { return _kotrinhUB; }
            set
            {
                _kotrinhUB = value;
                if (value == false && _trinhUB == false)
                {
                    TrinhUB = true;
                }

                OnPropertyChanged("KoTrinhUB");
            }
        }


        private bool _kodaXuLy;
        public bool KoDaXuLy
        {
            get { return _kodaXuLy; }
            set
            {
                _kodaXuLy = value;
                if (value == false && _daXuLy == false)
                {
                    DaXuLy = true;
                }

                OnPropertyChanged("KoDaXuLy");
            }
        }



        private readonly DataService _dataService;


        private ObservableCollection<HoSoChiTiet> _chiTiet;
        public ObservableCollection<HoSoChiTiet> ChiTiet
        {
            get { return _chiTiet; }
            set { SetProperty(ref _chiTiet, value); }
        }


        private string _TongDangXuLy;
        public string TongDangXuLy
        {
            get { return _TongDangXuLy; }
            set { SetProperty(ref _TongDangXuLy, value); }
        }


        private string _tongDaXuLy;
        public string TongDaXuLy
        {
            get { return _tongDaXuLy; }
            set { SetProperty(ref _tongDaXuLy, value); }
        }


        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set { SetProperty(ref _isBusy, value); }
        }


        private int _tongSo;
        public int TongSo
        {
            get { return _tongSo; }
            set { SetProperty(ref _tongSo, value); }
        }
        public HoSoChiTietViewModel()
        {
            _dataService = new DataService();
        }
        public async override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            var param = navigationParameter.ToString();
            TongDaXuLy = MainViewModel.ThongKeData.HoSo.DaGiaiQuyet;
            TongDangXuLy = MainViewModel.ThongKeData.HoSo.DangGiaiQuyet;

            TrinhUB = param[0] == '1' || param[0] == '2';
            DaXuLy = param[1] == '1' || param[1] == '2';
            DungHan = param[2] == '1' || param[2] == '2';
            KoTrinhUB = param[0] == '0' || param[0] == '2';
            KoDaXuLy = param[1] == '0' || param[1] == '2';
            KoDungHan = param[2] == '0' || param[2] == '2';

            await GetValue(param);

            this.PropertyChanged += HoSoChiTietViewModel_PropertyChanged;
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
        }

        private string GetParam()
        {
            string result = "";
            result = (TrinhUB && KoTrinhUB) ? "2" : (TrinhUB ? "1" : "0");
            result = result + ((DaXuLy && KoDaXuLy) ? "2" : (DaXuLy ? "1" : "0"));
            result = result + ((DungHan && KoDungHan) ? "2" : (DungHan ? "1" : "0"));

            return result;
        }
        void HoSoChiTietViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "TrinhUB":
                case "DaXuLy":
                case "DungHan":
                case "KoTrinhUB":
                case "KoDaXuLy":
                case "KoDungHan":
                    GetValue(GetParam());
                    break;

            }

        }

        public void UpdateView()
        {
            OnPropertyChanged("TrinhUB");
            OnPropertyChanged("DaXuLy");
            OnPropertyChanged("DungHan");
            OnPropertyChanged("KoTrinhUB");
            OnPropertyChanged("KoDaXuLy");
            OnPropertyChanged("KoDungHan");
        }

        private async Task GetValue(string param)
        {
            if (IsBusy) return;

            IsBusy = true;
            this.PropertyChanged -= HoSoChiTietViewModel_PropertyChanged;
            UpdateView();
            var list = await _dataService.HoSo(param[0], param[1], param[2]);
            int i = 1;
            list.ForEach(x => x.SoTT = i++);
            ChiTiet = new ObservableCollection<HoSoChiTiet>(list);
            TongSo = ChiTiet.Count;
           
            this.PropertyChanged += HoSoChiTietViewModel_PropertyChanged;
            IsBusy = false;
            if (param != GetParam())
                await GetValue(GetParam());
        }

        private List<TinhTrangXL> _tinhTrangXl;
        public List<TinhTrangXL> TinhTrangXl
        {
            get { return _tinhTrangXl; }
            set { SetProperty(ref _tinhTrangXl, value); }
        }
        public async Task TinhTrangXuLy(IEnumerable<object> enumerable)
        {
            dynamic t = enumerable.FirstOrDefault();
            TinhTrangXl = new List<TinhTrangXL>();
            if (t != null)
            {
                IsBusy = true;
                TinhTrangXl = await _dataService.TrangThaiHoSo(t.Sobnhan);
                await Task.Delay(1000);
                IsBusy = false;
            }


        }
    }
}
